package cn.bupt.adt.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.dao.autogenerate.CarouselMapper;
import cn.bupt.adt.entity.autogenerate.Carousel;
import cn.bupt.adt.entity.autogenerate.CarouselExample;
import cn.bupt.adt.service.ICarouselService;
import cn.bupt.adt.util.ImageUtil;
@Service
public class CarouselService implements ICarouselService {

	@Autowired
	CarouselMapper carouselMapper;
	@Override
	public List<Carousel> getAllCarousels() {
		return carouselMapper.selectByExample(new CarouselExample());
	}
	@Override
	public boolean modifyCarousel(HttpServletRequest request, int id) {
		MultipartHttpServletRequest req =  (MultipartHttpServletRequest) request;
		String rootPath = request.getSession().getServletContext().getRealPath("")+"/../"+Constants.IMAGE_PATH;
		String timePath = System.currentTimeMillis()+"/";
		MultipartFile carousel = req.getFile("carousel");
		Carousel car = carouselMapper.selectByPrimaryKey(id);
		if(car != null) {
			car.setSrc(ImageUtil.uploadImage(carousel, Constants.Carousel_PATH+timePath, rootPath, 1));
		}
		return carouselMapper.updateByPrimaryKeySelective(car) == 1;
	}

}
